package com.excellent.item.service.impl;

import com.excellent.item.mapper.CategoryMapper;
import com.excellent.item.pojo.Category;
import com.excellent.item.pojo.Sku;
import com.excellent.item.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品种类
 * @author xinang
 * @date 2020-02-22 21:43
 */
@Slf4j
@Service("CategoryService")
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据父节点 ID 查询子节点
     * @param pid
     * @return
     */
    @Override
    public List<Category> queryCategorysByPid(Long pid) {
        Category category = new Category();
        category.setParentId(pid);
        return this.categoryMapper.selectOrderBySort(category);
    }

    /**
     * 根据3级分类id，查询1~3级的分类
     * @param id
     * @return
     */
    public List<Category> queryAllByCid3(Long id) {
        Category c3 = this.categoryMapper.selectByPrimaryKey(id);
        Category c2 = this.categoryMapper.selectByPrimaryKey(c3.getParentId());
        Category c1 = this.categoryMapper.selectByPrimaryKey(c2.getParentId());
        return Arrays.asList(c1,c2,c3);
    }

    /**
     * 新增商品分类
     * 【如果改节点为叶子节点，就需要先将该节点的 isParent 的值改为 true】
     * @param category
     */
    @Override
    @Transactional
    public void saveCategory(Category category) {
        log.info(category.toString());
        this.categoryMapper.selectOrderBySort(category);
        /*
         * 如果该节点的父节点原先是叶子节点，就需要将它的父节点的 idParent 的值改为 true；
         */
        Category parentCategory = this.categoryMapper.selectByPrimaryKey(category.getParentId());
        if(parentCategory.getIsParent() == false){
            parentCategory.setIsParent(true);
            this.updateCategory(parentCategory);
        }
        this.categoryMapper.insertSelective(category);
    }

    /**
     * 修改商品类目信息
     * */
    public void updateCategory(Category category) {
        this.categoryMapper.updateByPrimaryKey(category);
    }

    @Override
    public int deleteCategory(Long id) {
        return this.categoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据分类 id 的集合 查询分类的名称的集合
     * @param ids
     * @return
     */
    @Override
    public List<String> queryNamesByIds(List<Long> ids) {
        List<Category> categoriesList = this.categoryMapper.selectByIdList(ids);
/*      List<String> nameList = new ArrayList<>();
        for (Category category : categoriesList) {
            nameList.add(category.getName());
        }
        return nameList;
*/
        // 等效的写法
        return categoriesList.stream().map(category -> category.getName()).collect(Collectors.toList());
    }

    @Override
    public List<Category> queryByBrandId(Long brandId){
        return this.categoryMapper.queryByBrandId(brandId);
    }
}
